<template>
  <div>
    <el-form-item label="节点唯一标识">
      <el-input v-model="localData.nodeKey" placeholder="请输入唯一标识" />
    </el-form-item>
    <el-form-item label="节点配置选项">
      <div>标题字段: <el-input v-model="localData.treeProps.label" /></div>
      <div>子节点字段: <el-input v-model="localData.treeProps.children" /></div>
      <div>禁用字段: <el-input v-model="localData.treeProps.disabled" /></div>
    </el-form-item>
    <el-form-item label="默认展开所有节点" label-width="140px">
      <el-switch v-model="localData.defaultExpandAll" />
    </el-form-item>
    <el-form-item label="树节点筛选" label-width="140px">
      <el-switch v-model="localData.nodeFilter" />
    </el-form-item>
    <el-form-item label="数据配置">
      <span style="margin-right: 8px">{{ getDictType }}</span>
      <text-edit-button text="配置" @click="openDictEdit" />
    </el-form-item>

    <dynamic-tree-setting v-model="visible" :data="localData" :options="formJson" @submit="handleDictUpdate" />
  </div>
</template>

<script>
import dynamicTreeSetting from './dynamicTreeSetting.vue';
export default {
  name: 'DynamicTreeConfig',
  components: { dynamicTreeSetting },
  props: {
    data: {
      type: Object,
      default: () => {}
    },
    formJson: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      localData: {},
      visible: false
    };
  },
  computed: {
    fields() {
      return this.getCallableCompList(this.formJson);
    },
    getDictType() {
      switch (this.localData.dicOption) {
        case 'static':
          return '静态数据';
        case 'remote':
          return '接口获取';
        default:
          return '静态数据';
      }
    }
  },
  watch: {
    data: {
      handler(newVal) {
        if (newVal) {
          this.localData = newVal;
        }
      },
      deep: true,
      immediate: true
    },
    localData: {
      handler(newVal) {
        this.$emit('update:data', newVal);
      },
      deep: true
    }
  },
  methods: {
    openDictEdit() {
      this.visible = true;
    },
    handleDictUpdate(data) {
      for (let key in data) {
        this.localData[key] = data[key];
      }
    }
  }
};
</script>
